Sections
RTOS is Real Time Operating System. RTOS is a multitasking operating system specially designed for real-time applications. In RTOS, the correctness of the system not only depends on the output of the system, but also depends on the instant at which the output is obtained. We can split RTOS as RT (Real Time) + OS (operating system). Real time is the actual time during which an event or process occurs and the Operating system provides an interface between hardware and application programs. There are many features that an operating system provides to the user. It includes Multitasking, Interrupt and Event handling, Synchronization, Inter-task Communication, Timers and Clock and various other features. So we can say that RTOS is an operating system that are specially designed to support real-time applications by providing logically correct output within the time period and having high degree of reliability.
RTOS architecture depends on the complexity of its deployment. Usually for simple applications, RTOS comprises only of a Kernel, and as the complexity of the embedded system increases, combination of various modules such as networking stacks, device drivers are integrated to it.
Kernel can be defined as the center module of a computer operating system. Kernel manages the communication between user level applications and the hardware. Kernel task include process management, Device management, Memory management, Interrupt handling etc. These tasks are done in the kernel space in OS. There are mainly three board categories of kernel models, namely:
1. Monolithic kernel
2. Microkernel
3. Exokernel
Monolithic Kernel runs all the basic system services such as Interprocess communication, process and memory management, interrupt handling I/O communication, file systems etc. in the kernel space. Earlier in monolithic kernel architecture, all the basic system services were packaged into a single module in kernel space. This led to some serious drawbacks such as size of the kernel becomes large and for bug fixing or addition of new features, we want recompile the whole kernel which could consume hours. But in the new approach, the kernel is having different modules which can be dynamically loaded and un-loaded. Thus the maintainability becomes very easy loading and unloading only the concerned module. Linux is an example of monolithic kernel.
Microkernel runs only the basic I/O control and process communication and the other basic system services such as file system, networking etc. will run on the user space. So in this architecture, the kernel space will be minimal and the user space will be large. This architecture is more stable than Monolithic. AmigaOS and QNX are the examples of Microkernel.
Exokernel is an operating system that provides application-level management of physical resources. Exokernel is extremely small and provides efficient control over the hardware. RTOS generally avoids using monolithic architecture since it uses large kernel space. The RTOS kernel is developed by micro-kernel with adding configurable functionalities since it uses less kernel space. RTOS kernel provides an abstraction layer between the hardware and application software. Kernel provides mainly six types of services to the application software namely:
1. Task Management
2. Interrupt and Events Handling
3. Memory Management
4. Device I/O Management
5. Timer Management
6. Task Synchronization and Intertask Communications
Task management helps the developers to design the software as separate “chunks” of codes so that each can handle a distinct goal and deadline.
Tasks are small, sequential programs that are scheduled so that concurrency can be achieved in real-time applications. Task is having 3 main time critical properties namely, Release Time, Deadline, Execution Time.
Task may be in any of the four states namely
1. Running
2. Ready
3. Blocked
4. Dormant
It schedules each task depending on the current state of each task and allocates CPU for it. It helps to reduce the waiting time and helps to improve CPU usage in a multitasking program. Schedulers generally can be of two types: non-preemptive and priority-based preemptive. In priority based preemptive scheduling the current running task is interrupted and a higher priority task will run for certain time if it occurs. In non-preemptive scheduling the current task will not terminate until its execution get completed. That means it cannot be interrupted.
The dispatcher will give the control of the CPU to the task which was selected by the scheduler from the current running task. It will save the content of current running task and reload with the contents of new task.
Task Synchronization & Intertask Communication helps to transmit information from one task to another task safely.
Synchronization is essential for sharing mutually exclusive resources among the task and to execute multiple tasks concurrently. This can be achieved by 2 mechanisms;
1. Event objects
2. Semaphores
If we are not sharing the resources and we want task synchronization, we use event objects. For a specific event to occur, one or more tasks may be waiting. An object event can be in any of the two states:
1. Triggered
2. non-triggered
In triggered state, waiting task may resume when an event occurs, while in non-triggered state, the waiting task remains suspended.
There are some cases where one task is using a resource while the other task is modifying or editing it. In those cases the result will be corrupted data. In order to resolve the problem, we use the concept of semaphore which is having resource count and wait queue. The resource availability is shown by resource count. The task that waits for the resources are managed by wait queue. There are 3 types of semaphore:
1. Binary Semaphores
2. Counting Semaphores
3. Mutually Exclusion Semaphore
This involves the sharing of resources among different tasks by sharing memory space, sending data etc. Message queues, pipes and Remote procedural calls (RPC) are some of the mechanisms available for executing intertask communication.
Usually embedded RTOS are trying to achieve small footprint. This can be achieved by including only the functionalities that the user needs. Stack and Heap management are the two types of memory management in RTOS.
In order to provide scheduling for each task, both user and system, a timer in needed. It can be relative timer, which works in units of ticks and absolute timers which work with calendar date and time.
One of the main challenges in RTOS design is the interrupt handling. It must be done with care to allow asynchronous access to internal RTOS data structures. Defining the interrupt handler, Creation, deletion and referencing the state of an ISR, Enabling and disabling of an interrupt, changing and referencing of an interrupt mask are main functions that the RTOS interrupt and event handling mechanism providing.
Embedded systems support large no of device drivers. This is made possible with the device I/O management equipped with the RTOS kernel. But most device driver APIs are standard i.e. only within a specific RTOS.
In hard RTOS, it is compulsory to meet the deadline within the time limit. Missing the deadline may leads to catastrophic events. So whenever an event happens, it should be serviced within the time. For example consider the anti-missile defense system of a country. It has to process within the stipulated time; else it can have very bad consequences.
In Soft RTOS, a small deviation from the deadline is allowable. Missing the deadline will not leads to any to catastrophic events. Here system quality reduction is acceptable. For example, consider the communication through mobile phones. If there is any delay, then there will not be any catastrophe. But the communication becomes unpleasant.
The important service of RTOS is Task Management. Services in this category mainly include the ability to launch tasks and assign priorities. The main RTOS service in this category is the scheduling of different tasks. The Task Scheduler maintains the execution of application software tasks and makes them to run in time responsively.
The second category of service is Inter task Communication and Synchronization. This service provides tasks to pass information from one to another one, without any information loss. Here make it possible for task to coordinate so that it can cooperate with one another.
Since most embedded systems have got timing requirements, most RTOS are provided with basic Timer services, such as time-outs and task delays.
RTOS kernels provide Dynamic Memory Allocation services. This category of service allows tasks to borrow files from RAM memory for temporary use in application software. Often these files from memory are then passed from task to task, as a fast means of communication among huge amounts of data between tasks.
RTOS kernels also give a Device I/O Supervisor category of services. The various services provided are uniform framework for accessing and organizing the different hardware device drivers of an embedded system.
RTOSs give a number of optional add-on operating system components for services which are high-level such as file system organization network management, database management, network communication, user-interface graphics, etc.
The Tasks must have sufficient Inter Task Communication so that data integrity can be ensured.
The time required to save the currently executing task and switching to another task should be minimum as possible.
It is the capability to identify which Task needs the most suitable resource.
The interrupt services in RTOS are generally done in very fast manner.
RTLinux and VxWorks are some examples of RTOS that uses embedded linux platform.
VxWorks is RTOS (Real Time Operating System) developed by Wind River. It is a high performance, Unix like, scalable OS (only the necessary OS functions become part of application codes). It has got multitasking environment using scheduler. A preemptive scheduling is employed which means that tasks will be done according to their priority basics. It means that scheduler preempts CPU when a higher priority task comes while current task is running. VxWorks supports the ability to run two concurrent Oss on a single processing layer. There is a multiple file system and system which provides advanced multimedia functionality. It has got different context saving mechanism for task and ISR (Interrupt Service Routine). It has both virtual memory management and power management functions.
VxWorks basic functions
System level
Task service function
Task control function
Inter Process Communications- Semaphore, Queue, Pipes
Network functions
IO functions
While executing the program it is called as Task. VxWorks real time kernel, wind gives multitasking environment. Each task has got context where CPU environment and system resources that the task sees each time it is scheduled to run by kernel. Kernel maintains current state of each task in system. When created task will be in suspended state. After activation it will enter ready state.
Task State symbol
READY
PEND
DELAY
SUSPEND
DELAY+S
PEND+S
PEND+T
PEND+S+T
Wind Task Scheduling
The default algorithm used is preemptive scheduling. But you can also use round-robin scheduling for your applications. Wind kernel has got 256 priority levels which are numbered from 0-255, where 0 has highest and 255 has lowest priority.
Task Priority set
kernel time slice()- controls round-robin
task priority set()- changes priority of a task
task lock()- disables task rescheduling
task unlock()- enables task rescheduling
Task creation routines
task spawn()- creates and activates new task
task Init()- initializes a new task
task Activate()- activates an initialized task
Task Name and ID Routines
task Name()- returns the task name that associates with taskID passed as argument
task Name Told()- Looks up the task ID associated with a task name
task Id self()- returns the taskID of the task
task Id verify()- verifies if a task of taskID in the argument is available
Sample code for VxWorks device driver #include "vxWorks.h" #include "ioLib.h" int serial_test_main() { int m = 0; int hd = 0; char bib_name[] = "/tyCo/0"; char hi[] = "Mepits\n"; int hi_len = 13; hd = open( "/tyCo/0", O_RDWR, 0); m = write( hd, hi, hi_len); close( hd ); return 0; } The output will be Mepits. It is printed without using printf function. Sample code for watch dog timer #include "vxWorks.h" #include "stdio.h" #include "logLib.h" #include "sysLib.h" #include "wdLib.h" WATCHDOG_ID myWatchDog; int j = 0 ; int sound(int count) { if (count == 0) { printf("Hello!!\n"); return (0) ; } printf("(%d)Sound!\n",count); if (wdStart (myWatchDog, sysClkRateGet( ) * count, sound, count-1)== ERROR) return count ; } int watchdog (int delay) { printf("WatchDog Starting...\n"); if ((myWatchDog = wdCreate( )) == NULL) return (9999) ; sound(delay) ; return 0 ; }
This produces a watch dog timer which is self-schedulable. It schedules itself for delay, delay-1, delay-2... 0 seconds. In the first block of function, routine sets up a timer and it counts down from delay and then prints “Hello” after delay repetitions. Each repetition is set for one less second than the previous. It will also test to see if it has counted down to zero, if fails it will reschedule itself for a delay of count-1 seconds. The function wdStart() initialize the watchdog timer, myWatchDog is the timer employed. Function sysClockRateGet() * count, sets the time to be count seconds, bark is the function to invoke on timeout, while count-1 is the parameter to pass to the function. The function wdStart will produce an error code which indicates success or failure. The last block is the main function used to display the message, create watch dog timer and also call sound for first time.
RTLinux is a small hard real-time OS microkernel, which is having a higher priority than the Linux kernel. The RTLinux kernel works beneath Linux. Real-time kernel executes real-time tasks such as deterministic interrupt latency ISRs and other non-real-time tasks such as in deterministic task processing are moved to Linux kernel. In RTLinux, the entire OS runs as a fully preemptive process. The Linux kernel is modified by adding a virtual machine layer in between the computer hardware and standard linux kernel. Here the Linux functions are moved to a FIFO. The FIFO memory is shared between RTLinux and Linux. In this FIFO, RTLinux programs are having higher priority and Linux functions are having lower priority. While running, real-time tasks will disable the interrupts thus reducing the unpredictability. RTLinux requires a 4MB footprint. RTLinux supports many features such as root-level security, it prevents unauthorized port access, registering and initialization of modules as well as threads, scheduling, etc.
Programming Tips
1. RTLinux uses the kernel space for executing the programs and there will be not be any protection against errors in the user's code. Programming errors may lead to system down.
2. Use the debugger which the RTLinux supplies in order to reduce the risk of system crashes.
3. If the floating point unit is required in the task, we must explicitly set permissions, because by default RTLinux tasks are not having floating point unit access.
4. Command prompt arguments can't be passed in RTLinux.
#include <rtl.h> #include <time.h> #include <pthread.h> pthread thread_1; int init_module(void) { return pthread_create(&thread_1,NULL,watch,0) } void cleanup_module(void) { pthread_cancel(thread_1); pthread_join(thread,NULL); } void *watch(void *arg) { struct sched_param wat; hrtime_t now; wat . sched_priority = 1; pthread_setschedparam (pthread_self(), SCHED_FIFO, &wat); pthread_make_periodic_np (pthread_self(), gethrtime(), 100000000); while (1) { pthread_wait_np (); now = clock_gethrtime(CLOCK_REALTIME); rtl_printf("Time = %Ld\n",(long long)now); } return 0;
Sections